# this is write by qingluan
# just a inti handler
# and a tempalte offer to coder
import json
import tornado
import tornado.web

from termcolor import cprint
from tornado.websocket import WebSocketHandler


class BaseHandler(tornado.web.RequestHandler):

    def prepare(self):
        self.db = self.settings['db']

    def get_current_user(self):
        return (self.get_cookie('user'), self.get_cookie('passwd'))

    def get_current_secure_user(self):
        return (self.get_cookie('user'), self.get_secure_cookie('passwd'))

    def set_current_seccure_user_cookie(self, user, passwd):
        self.set_cookie('user', user)
        self.set_secure_cookie("passwd", passwd)


class SocketHandler(WebSocketHandler):
    """ Web socket """
    clients = set()
    con = dict()

    @staticmethod
    def send_to_all(msg):
        for con in SocketHandler.clients:
            cprint("-> %d:" % id(con) + str(msg))
            con.write_message(json.dumps(msg))

    @staticmethod
    def send_to_one(msg, id):
        SocketHandler.con[id(self)].write_message(msg)


    def open(self):
        self.write_message(json.dumps({
            "type": "sys",
            "code": "true",
        }))
        SocketHandler.clients.add(self)
        SocketHandler.con[id(self)] = self

    def on_close(self):
        SocketHandler.clients.remove(self)
        cprint(str(id(self)) + "closed", "cyan")

    def on_message(self, msg):
        cprint("[got]" + msg, "blue")
        SocketHandler.send_to_all(msg)


class IndexHandler(BaseHandler):

    def prepare(self):
        super(IndexHandler, self).prepare()
        self.template = "template/index.html"

    def get(self):
        return self.render(self.template, post_page="/")

    @tornado.web.asynchronous
    def post(self):
        # you should get some argument from follow
        # post_args = self.get_argument("some_argument")
        # .....

        # self.redirect()  # redirect or reply some content
        self.write("hello world")


class LocalHandler(BaseHandler):

    def prepare(self):
        super(LocalHandler, self).prepare()
        self.template = "template/local.html"

    def get(self):
        return self.render(self.template, post_page="/local")

    @tornado.web.asynchronous
    def post(self):
        # you should get some argument from follow
        # post_args = self.get_argument("some_argument")
        # .....

        # self.redirect()  # redirect or reply some content
        self.write("hello world")


class ActivityHandler(BaseHandler):

    def prepare(self):
        super(ActivityHandler, self).prepare()
        self.template = "template/activity.html"

    def get(self):
        return self.render(self.template, post_page="/activity")

    @tornado.web.asynchronous
    def post(self):
        # you should get some argument from follow
        # post_args = self.get_argument("some_argument")
        # .....

        # self.redirect()  # redirect or reply some content
        self.write("hello world")


class InteractHandler(BaseHandler):

    def prepare(self):
        super(InteractHandler, self).prepare()
        self.template = "template/interact.html"

    def get(self):
        return self.render(self.template, post_page="/interact")

    @tornado.web.asynchronous
    def post(self):
        # you should get some argument from follow
        # post_args = self.get_argument("some_argument")
        # .....

        # self.redirect()  # redirect or reply some content
        self.write("hello world")


class NodeHandler(BaseHandler):

    def prepare(self):
        super(NodeHandler, self).prepare()
        self.template = "template/scrapy.html"

    def get(self):
        return self.render(self.template, post_page="/scrapy")

    @tornado.web.asynchronous
    def post(self):
        print(self.request.body)
        # you should get some argument from follow
        post_args = json.loads(self.request.body.decode("utf8", "ignore"))['msg']
        # .....
        print(post_args)

        # print(self.get_argument())
        # self.redirect()  # redirect or reply some content
        SocketHandler.send_to_all(post_args)
        self.write("sending")
        self.finish()
